import pymysql

class HandData(object):
    def __init__(self):
        self.db = pymysql.connect('localhost', 'root', '123321', 'webcrawer')
        self.cursor = self.db.cursor()
        # create = """CREATE TABLE IF NOT EXISTS crawer (
        #  URL  TEXT,
        #  TITLE TEXT,
        #  CONTENT LONGTEXT)
        #  """
        # # 执行sql语句
        # self.cursor.execute(create)
        # # 执行sql语句
        # self.db.commit()

    def insert_data(self, data):

        sql = "INSERT INTO `webcrawer`.`crawer`(URL,TITLE, CONTENT,ID) VALUES ('{}','{}','{}',NOW())".format(data.url, data.title, data.summary)
        try:
            # 执行sql语句
            self.cursor.execute(sql)
            # 执行sql语句
            self.db.commit()
        except:
            # 发生错误时回滚
            self.db.rollback()

            # 关闭数据库连接
        self.db.close()
        return None
